/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.behaviours.world;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import java.io.IOException;

import ca.usherbrooke.agent.message.RequestMessageObjectWorldConfig;
import ca.usherbrooke.agent.world.BasicWorldAgent;
import ca.usherbrooke.behaviours.BasicRequestReponderBehaviour;

/**
 * @author Cody Stoutenburg
 * 
 *         this behaviour allow us to change some configuration of the world
 * 
 */
public class WorldConfigurationResponderBehaviour extends
		BasicRequestReponderBehaviour<BasicWorldAgent> {
	private static final long serialVersionUID = 1L;

	public final static String REQUEST_CONFIG_PROTOCOL = "proto.request.config";

	public final static MessageTemplate REQUEST_TEMPLATE = MessageTemplate.and(
			MessageTemplate.MatchProtocol(REQUEST_CONFIG_PROTOCOL),
			MessageTemplate.MatchPerformative(ACLMessage.REQUEST));

	/**
	 * @param a
	 */
	public WorldConfigurationResponderBehaviour(BasicWorldAgent a) {
		super(a, REQUEST_TEMPLATE, REQUEST_CONFIG_PROTOCOL);
	}

	@Override
	protected ACLMessage prepareResult(ACLMessage request) {
		ACLMessage response = this.buildResultMessage(request);

		try {
			RequestMessageObjectWorldConfig<?> message = (RequestMessageObjectWorldConfig<?>) request
					.getContentObject();

			switch (message.getRequestType()) {
			case RequestMessageObjectWorldConfig.REQUEST_START_SIMULATION:
				this.getAgent().startUpdate();
				response.setContent("simulation started");
				break;
			case RequestMessageObjectWorldConfig.REQUEST_GET_SIMULATION_DELTA:
				Integer delta = this.getAgent().getDelta();
				try {
					response.setContentObject(new RequestMessageObjectWorldConfig<Integer>(
							RequestMessageObjectWorldConfig.REPLY_GET_SIMULATION_DELTA,
							delta));
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				break;
			case RequestMessageObjectWorldConfig.REQUEST_SET_SIMULATION_DELTA: {
				Integer newDelta = (Integer) message.getValue();
				this.getAgent().setDelta(newDelta);
				response.setContent("delta set");
				break;
			}
			case RequestMessageObjectWorldConfig.REQUEST_INC_SIMULATION_DELTA: {
				Integer newDelta = (Integer) message.getValue()
						+ this.getAgent().getDelta();
				if (newDelta > 0) {
					this.getAgent().setDelta(newDelta);
					response.setContent("delta set");
				} else {
					response.setContent("delta not set");
				}
				break;
			}
			case RequestMessageObjectWorldConfig.REQUEST_PROPERTIES_PATH:
				response.setContent(getAgent().getSimulation()
						.getPropertiesPath());
				break;
			case RequestMessageObjectWorldConfig.REQUEST_WORLD_MODEL:
				try {
					response.setContentObject(getAgent().getSimulation()
							.getWorld());
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				break;
			default:
				System.out.println("unknow message receive");
				break;
			}
		} catch (UnreadableException ex) {
			ex.printStackTrace();
		}

		return response;
	}
}
